﻿#include "logger\FileSink.h"

#include <fstream>

#include "logger\LogMessageInfo.h"

namespace Logging {
class FileSink::Impl {
 public:
  std::ofstream file_stream_;
  Impl(const std::string& filepath) : file_stream_(filepath, std::ios::app) {}
};

FileSink::FileSink(const std::string& filepath)
    : pimpl_(std::make_unique<Impl>(filepath)) {}

FileSink::~FileSink() = default;

void FileSink::Log(const LogMessageInfo& message) {
  if (pimpl_->file_stream_.is_open()) {
    // ★★★ 核心简化：只写入消息内容，并添加换行符 ★★★
    pimpl_->file_stream_ << message.message << std::endl;
  }
}

void FileSink::Flush() {
  if (pimpl_->file_stream_.is_open()) {
    pimpl_->file_stream_.flush();
  }
}
}  // namespace Logging